package com.login.controller;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.aop.annotaions.FilterAnnotaions;
import com.login.model.LoginDo;
import com.login.service.LoginService;

@Controller
@RequestMapping("/login/*")
public class LoginController {
	private Logger log = Logger.getLogger(LoginController.class);
	//进行测试代码提交看看
	@Autowired
	private LoginService loginService;
	
	
	@FilterAnnotaions(aaa = "11")
	@RequestMapping("/login")
	public String  login(HttpSession session,ModelMap map,LoginDo l,HttpServletRequest request){
		List<LoginDo> list = loginService.findLogin(l);
		 LoginDo alreadyLogin =  (LoginDo)request.getSession().getAttribute("user");   
		 if(alreadyLogin != null){
			 return "/index"; 
		 }
		 String userName=l.getUserName();
		 if(StringUtils.isEmpty(userName)){
			 session.setAttribute("user", l);
			 return "/index"; 
		 }
		if(CollectionUtils.isEmpty(list)){
			//如果为空
			 return "/error"; 
		}
		session.setAttribute("user", l);
		 return "/index"; 
		  
	}
	@FilterAnnotaions(aaa = "333")
	@ResponseBody
	@RequestMapping("/phoneLogin")
	public String  phoneLogin(HttpSession session,ModelMap request,LoginDo l){
		List<LoginDo> list = loginService.findLogin(l);
	//	session.setAttribute("user", l);
		
		if(CollectionUtils.isEmpty(list)){
			//如果为空
			 return " error"; 
		}
		 
		session.setAttribute("user", l);
		
		 return " index"; 
		  
	}
	public static void main(String[] args) throws DateParseException {
	//	 Optional.of("").ifPresent(x);


	}
}
